/*
Copyright (c) 2009, Heinrich C. Spreiter
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

  - Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
  
  - Redistributions in binary form must reproduce the above copyright notice,
  this list of conditions and the following disclaimer in the documentation
  and/or other materials provided with the distribution.
  
  - Neither the name of the author nor the names of its contributors may be used
  to endorse or promote products derived from this software without specific
  prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

/*
 * SymbolTable.java
 *
 * @version 0.1 28.12.2009
 * @author HSP spreiter301@gmail.com
 * 
 * Defines all the symbols in the symbol table
 */
 
 tree grammar DefineSymbols;
 
 options {
  language=Java;
  filter=true;
  backtrack=true;
  tokenVocab=KohYaoNoi;
  ASTLabelType=KynTree;
}

@header {
package ch.kynlang.compiler;
import ch.kynlang.compiler.tree.*;
import ch.kynlang.compiler.symbol.*;
import ch.kynlang.compiler.scope.*;
import ch.kynlang.compiler.type.*;
import java.util.Stack;
}

@members {
// tracks all symbols
private SymbolTable st = null;

private IScope currentScope = null;

private Stack<ContainerSymbol> containers = null;

public DefineSymbols(TreeNodeStream input, SymbolTable st) {
  this(input);
  this.containers = new Stack<ContainerSymbol>();
  this.st = st;
  this.currentScope = this.st.rootScope;
}
}

topdown
  : packageDef
  | importDef
  | varDef
  | macroDef
  | widgetDef
  ;
  
bottomup
  : widgetDefExit
  ;
  
packageDef
  : ^(PACKAGE IDENTIFIER+)
    {
      $PACKAGE.scope = currentScope;
      PackageSymbol ps = new PackageSymbol($PACKAGE);
      currentScope.define(ps);
    }
  ;
 
  
importDef
  : ^(AS IDENTIFIER IDENTIFIER+)
    {
      $AS.scope = currentScope;
      ImportSymbol is = new ImportSymbol($AS);
      boolean success = currentScope.define(is);
      if(!success) {
        st.error(new KohYaoNoiError($AS, "Import alias " + is.name + " already defined"));
      }
    }
  | ^(IMPORT IDENTIFIER+)
    {
      $IMPORT.scope = currentScope;
      ImportSymbol is = new ImportSymbol($IMPORT);
      boolean success = currentScope.define(is);
      if(!success) {
        st.error(new KohYaoNoiError($IMPORT, "Import " + is.name + " already defined"));
      }
    }
  ;
  
varDef
  : ^(DEF IDENTIFIER .)
    {
      $DEF.scope = currentScope;
      VariableSymbol vs = new VariableSymbol($DEF);
      boolean success = currentScope.define(vs);
      if(!success) {
        st.error(new KohYaoNoiError($DEF, "Variable name " + vs.name + " already in use"));
      }
    }
  ;
  
macroDef
  : ^(HASH IDENTIFIER ^(PBODY .+))
    {
      $HASH.scope = currentScope;
      MacroSymbol ms = new MacroSymbol($HASH);
      boolean success = currentScope.define(ms);
      if(!success) {
        st.error(new KohYaoNoiError($HASH, "Macro name " + ms.name + " already in use"));
      }
    }
  | ^(HASH IDENTIFIER PBODY)
    {
      $HASH.scope = currentScope;
      MacroSymbol ms = new MacroSymbol($HASH);
      boolean success = currentScope.define(ms);
      if(!success) {
        st.error(new KohYaoNoiError($HASH, "Macro name " + ms.name + " already in use"));
      }
    }
  ;
  
widgetDef
  : ^(WIDGET wtype=IDENTIFIER wname=IDENTIFIER  (PBODY|^(PBODY .+)) (WBODY|^(WBODY .+))?)
  {
    // push container widgets on a stack
    $WIDGET.scope = currentScope;
    ComponentSymbol c = ComponentSymbolFactory.createComponent($WIDGET);
    
    if(c != null) {
      boolean success = currentScope.define(c);
      if(success) {
        if(c instanceof ContainerSymbol && containers.size() == 0) {
          // root container
          // check if it can be root
          ContainerSymbol cs = (ContainerSymbol) c;
          if(cs.canBeRoot) {
	         containers.push(cs);
	        } else {
	         st.error(new KohYaoNoiError($WIDGET, "Widget " + c.name + " is not a valid root container"));
	        }
	      } else if(c instanceof UIComponent && containers.size() == 0) {
	       // we have no container for this UIComponent!
	       st.error(new KohYaoNoiError($WIDGET, "Widget " + c.name + " must be inside a container"));
	      } else if(containers.size() > 0) {
	       // there is at least 1 container on the stack
	       c.parentComponent = containers.peek();
	       // check if it's a container
	       if(c instanceof ContainerSymbol) {
	         containers.push((ContainerSymbol) c);
	       }
	      }
	      
      } else {
        st.error(new KohYaoNoiError($WIDGET, "Widget name " + c.name + " already in use"));
      }
    } else {
      // error!! 
      st.error(new KohYaoNoiError($WIDGET, $WIDGET.getChild(0).getText() + " is not a widget")); 
    }
  }
  ;
  
widgetDefExit
  : ^(WIDGET wtype=IDENTIFIER wname=IDENTIFIER  (PBODY|^(PBODY .+)) (WBODY|^(WBODY .+))?)
  {
    // pop container widgets from stack
    for(String type : ComponentSymbolFactory.CONTAINERS) {
      if($wtype.getText().equals(type) && containers.size() > 0) {
        containers.pop();
        break;
      }
    }
  }
  ;